        TITL DAI FIRMWARE 2EDF6-2EFFF  V1.1
        ORG   :EDF6
*
*
*
****************
* CURSOR RIGHT *
****************
*
* Moves cursor right one position.
*
* Exit: ABCDEHL preserved.
*       CY=0: Cursor not moved.
*       CY=1: Cursor moved.
*
ECRT    PUSH  D
        PUSH  H
        PUSH  PSW
        LDA   EWINX     Get offset of left side
 REM of window
        ADI   :3B       Calc end of window
        MOV   L,A       Result in L
        LDA   SCXBUF+8  Get X-offset of cursor
 REM in document
        CPI   :FF       Max nr char/line reached ?
        JZ    L2E180    Then quit with CY=0
        CMP   L         End of window reached ?
        CZ    EWRT      Then window right
        INR   A         Incr cursor pos on line
        STA   SCXBUF+8  Store X-offset of cursor
 REM in document
        XRA   A
        STA   SCXBUF+:0C Clear cursor pos in buffer
        LHLD  CURSOR    Get cursor pos addr
        CALL  CURDEL    Delete old cursor
        JMP   L2E189    Put new cursor on screen;
 REM quit with CY=1
*
*****************************
* SKIP LINES IN EDIT BUFFER *
*****************************
*
* Entry: HL: Number of lines to be skipped.
* Exit:  HL: Points to 1st not skipped line.
*        CY=1: O.K.
*        CY=0: End of text reached before count
*              is zero.
*        ABCDE preserved.
*
L2E191  PUSH  D
        PUSH  PSW
        XCHG            Count in DE
        LHLD  EBUFR     Get startaddr editbuf
L2E192  MOV   A,D       )
        ORA   E         ) Abort with CY=1 if ready
        JZ    L2E193    )
        CALL  L2E195    Next line
        NOP 
        ORA   A
        JZ    L2E194    Abort with CY=0 if end of
 REM buffer reached before DE=0
        DCX   D         Count -1
        JMP   L2E192    Skip next line
L2E193  STC 
L2E194  POP   D
        MOV   A,D
        POP   D
        RET 
*
*************
* NEXT LINE *
*************
*
* Entry: HL: points to text, each line ended by
*            a car.ret, last line followed by 0.
* Exit:  HL: Points to next line.
*        A:  0 if end of text reached, else CR.
*        BCDE preserved.
*
L2E195  MOV   A,M       Get char from text
        ORA   A
        JZ    L2E196    Ready if end of text reached
        INX   H
        CPI   :0D       Car.ret ?
        JNZ   L2E195    Loop till end of line found
L2E196  RET 
*
****************************************
* FIND CURRENT POSITION IN EDIT BUFFER *
****************************************
*
* Exit: CY=1: HL: (00AF/AE) = cursor position in
*                 buffer.
*       CY=0: HL: Points to cursor line in the
*                 edit buffer.
*       ABCDE preserved.
*
L2E197  LHLD  CURPT     Get pntr to cursor pos
 REM in buffer
        DCR   H
        INR   H
        STC 
        RNZ             Quit (CY=1) if (AF) <> 0
        PUSH  D
        PUSH  B
        PUSH  PSW
        LHLD  ECURY     Get Y-offset of cursor
 REM in document (no lines to
 REM be skipped)
        MOV   D,H       ) in DE
        MOV   E,L       )
        CALL  L2E191    Skip lines
        SHLD  CURLB     Store pntr to start cursor
 REM line in buffer
        PUSH  H         and preserve it
        LHLD  EWINY     Get offset of top of window
        CALL  SUBDE     Calc difference
        MVI   A,:86     Length one line
        CALL  HLMUL     Calc total length
        XCHG            Result in DE
        LHLD  CHS       Get startaddr char area
        DAD   D         Add offset
        SHLD  CURLS     Preserve pntr to start
 REM cursor line on screen
        POP   H         Get pntr to start cursor
 REM line in buffer
        LDA   SCXBUF+8  Get X-offset of cursor
 REM in document
        MOV   B,A       in B
        CALL  L2E198    Skip to Bth pos on line
 REM exit: HL pnts to cursor pos
        POP   B
        MOV   A,B
        POP   B
        POP   D
        CMC             Abort with CY=0
        RET 
*
*************************************
* SKIP TO Bth POSITION ON TEXT LINE *
*************************************
*
* Looks through a textline until Bth position is
* found. On exit, character on this position is
* in A.
*
* Entry: HL: Start text line.
*        B:  Number of position in line.
* Exit:  CY=0: Position found:
*              A:  Character on that position.
*              HL: Points to Bth position.
*              BCDE preserved.
*        CY=1: Bth position is beyond car.ret,
*              tab or end of text.
*              HL: Points to CR, tab or 0.
*              A:  Space.
*
L2E198  PUSH  B
        PUSH  D
        MVI   C,:00     Init count
L2E199  JMP   LD19      Evaluate character
L2E200  JZ    L2E204    Abort if Bth pos reached
        ORA   A         Set flags on char
        JZ    L2E203    Jump if end of text reached
        CPI   :0D
        JZ    L2E203    Jump if char is car.ret
        CPI   :09
        JZ    L2E202    Jump if char is tab
        INR   C         Incr count
L2E201  INX   H         Pnts to next pos on line
        JMP   L2E199    Loop untill ready
 REM
* If character is tab:
 REM
L2E202  CALL  L2E205    Tabulate
        MOV   A,B       Reqd pos in A
        CMP   C         Compare with tab stops
        JNC   L2E201    Continu if not past tab
 REM
* If end of line or end of text reached or if
* past tab:
 REM
L2E203  MVI   A,:20     Set char is space
        STC             Abort with CY=1
L2E204  POP   D
        POP   B
        RET 
*
************
* TABULATE *
************
*
* Routine goes through tab-table for 1st
* tab-stop > C.
*
* Entry: 00B4/B5:  Pointer to tab-table.
*        C:        Line position.
* Exit:  If found: Tab in C.
*        Else:     C = C + 1.
*        AFBDEHL preserved.
*
L2E205  PUSH  PSW
        PUSH  B
        PUSH  H
        LHLD  TABTP     Get addr tab table
L2E206  MOV   B,C       Line pos in B
        INR   B         +1
        MOV   A,M       Get tab from table
        ORA   A
        JZ    L2E207    If end tab table reached
        INX   H         Pnts to next tab
        MOV   B,A       Tab in B
        MOV   A,C       Line pos in A
        CMP   B
        JNC   L2E206    Get next tab if line
 REM pos > tab stop
L2E207  MOV   C,B       Replace line pos by tab stop
 REM or by line pos +1 if no tab
 REM found
        POP   H
        POP   PSW
        MOV   B,A
        POP   PSW
        RET 
*
********************************
* PRINT LINE OF TEXT IN WINDOW *
********************************
*
* Entry: HL:  Address of textline in buffer.
*        DE:  Line control byte of screen line
*             to print text on.
*        00A8: Number of non-printing positions.
*
* During execution loop:
*        B:   Nr of non-printing pos left +1.
*        C:   Nr of screen line pos left.
*        D:   Current text line position.
*        E:   Count of blanks inserted.
*        HL:  Points to text.
*        TOS: Points to screen.
*
L2E208  PUSH  B
        XCHG
        LXI   B,TICC8
        DAD   B         1st printable pos on screen
        XCHG            in DE
        LDA   EWINX     Get offset of left side
 REM of window
        INR   A
        MOV   B,A       B is offset +1
        MVI   C,:3C     60 visible characters
        PUSH  D         Preserve start screen line
        MVI   D,:00     ) Init current text pos
        MOV   E,D       ) and blanks count
 REM
* Loop:
 REM
L2E209  MVI   A,:20     Space
        DCR   E
        INR   E
        JNZ   L2E212    E<>0: Update screen pos pntr
        MOV   A,M       Else: Get char from buffer
        ORA   A         Set flags on char
        JMP   LD15      Test character
L2E210  CPI   :0D
        JZ    L2E212    Skip tab-handling if CR
        MVI   E,:00     Reset blanks count
        INX   H         Pnts to next char in text
        CPI   :09       Tab ?
        JZ    L2E213    Then tab-handling
L2E211  INR   E
L2E212  DCR   E         Update blanks count
        INR   D         Update line position
        DCR   B         and nr of pos left in window
        JNZ   L2E209    No printing, cont with
 REM next char
        INR   B
        XTHL            Screen pos in HL
        MOV   M,A       Display char on screen
        DCX   H
        DCX   H         Next screen pos
        XTHL            Preserve it
        DCR   C         End of screen line reached ?
        JNZ   L2E209    Next char if not
        CALL  L2E195    Else: next line
        XTHL            HL is screen pos
        LXI   D,:FFFA
        DAD   D         HL is 1st useable pos
 REM on next line
        XCHG            into DE
        POP   H
        POP   B
        RET 
 REM
* Tab-handling:
 REM
L2E213  PUSH  B
        MOV   C,D       C is pos on current line
        CALL  L2E205    Tabulate
        MOV   A,C       Next tab stop in A
        SUB   D         ) Calc blanks to be inserted
        DCR   A         )
        MOV   E,A       Update blanks count
        POP   B
        MVI   A,:09     Restore tab char in A
        JMP   L2E211
*
***************************
* PRINT A COMPLETE WINDOW *
***************************
*
* Text is printed in window from (DE) to bottom
* text screen if text ends. A ASCII 0 (vertical
* bar) is printed at the beginning of all
* following lines.
*
* Entry: HL: Points to text.
*        DE: Points to line control byte screen line
* Exit:  HL: Points to next line to print.
*        DE: Bottom text screen.
*        AF corrupted, BC preserved.
*
L2E214  PUSH  H
        LHLD  GAE       Get bottom text screen
        CALL  COMP      Compare HL-DE
        POP   H
        JZ    L2E215    Quit if window full
        CALL  L2E208    Print a textline
        JMP   L2E214    Loop until ready
L2E215  RET 
*
***************************************
* PRINT A TEXT LINE POINTED BY (00B2) *
* ON SCREEN POSITION (00B0) IN WINDOW *
***************************************
*
* Exit: All registers preserved.
*
L2E216  PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        LHLD  CURLS     Get pntr to start cursor
 REM line on screen
        XCHG            in DE
        LHLD  CURLB     Get pntr to start cursor
 REM line in buffer
        CALL  L2E208    Print a text line
        JMP   XRET      Popall, ret
*
*********************
* PRINT FULL SCREEN *
*********************
*
* Prints from Nth line in full screen window.
* N is the offset of the top of the window from
* the start of the edit buffer.
*
* Exit: All registers preserved.
*
L2E217  PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        LHLD  CHS       Get startaddr char area
        XCHG            in DE
        LHLD  EWINY     Get offset of top of window
        CALL  L2E191    Skip lines
        JMP   LC216     Print complete window
*
*
*
******************************
* INSERT CHARACTER IN BUFFER *
******************************
*
* Entry: Character in A.
* Exit:  ABCDEHL preserved.
*        CY=0: Buffer full, no action.
*        CY=1: Character inserted.
*
EINCH   PUSH  B
        PUSH  PSW
        PUSH  D
        PUSH  H
        MOV   B,A       Char in B
        LHLD  EBUFS     Get end available space
        XCHG            in DE
        LHLD  EBUFN     Get input pointer
        CALL  COMP      Compare HL-DE
        XCHG            Input pntr in E
        MOV   A,B       Char in A
        CC    L2E197    Space available: Find
 REM current pos in buffer
        JNC   L2E219    Buffer full: quit, char in A
        PUSH  H         Preserve end available space
        LHLD  CURSOR    Get cursor pos addr
        XTHL            Put it on stack
        PUSH  H
        CALL  CURDEL    Delete cursor
        XCHG            Old input pntr in HL
        NOP 
        INX   H         +1
        SHLD  EBUFN     Update input pointer
        DCX   H
        MOV   B,H       ) Old input pntr in BC
        MOV   C,L       )
        DCX   H
        XCHG            Old input pntr -1 in DE
        POP   H         Get end available space
        DCX   H         -1
        CALL  MOVES     Move screen area 1 pos
        INX   H
        MOV   M,A       Insert char in buffer
        INX   H
        SHLD  CURPT     Preserve pntr to cursor
 REM in buffer
        POP   H         Get end available space
        CPI   :0D
        JZ    L2E220    Jump if char is car.ret
        CALL  L2E216    Reprint text line in window
        CALL  CURSET    Put cursor on screen
        CPI   :09
        JZ    L2E230    Jump if char is tab
        CALL  ECRT      Move cursor right 1 pos
L2E218  STC 
L2E219  POP   H
        POP   D
        POP   B
        MOV   A,B
        POP   B
        RET 
 REM
* If car.ret:
 REM
L2E220  XRA   A
        STA   EWINX     )
        STA   SCXBUF+8  ) Reset pointers
        STA   SCXBUF+:0C     )
        CALL  L2E217    Reprint full screen
        LHLD  CURLS     Get pntr to start cursor
 REM line on screen
        LXI   D,TICC8
        DAD   D         New cursor addr
        CALL  CURSET    Put cursor on screen
        CALL  ECDN      Move cursor down
        JMP   L2E218    Quit with CY=1
 REM
* If tab:
 REM
L2E230  CALL  ECRT      Move cursor right
        LDA   SCXBUF+8  Get X-offset of cursor
 REM in document
        MOV   B,A       in B
        LHLD  CURLB     Get pntr to start cursor
 REM line in buffer
        CALL  L2E198    Skip to Bth pos on line
        JC    L2E230    Again
        JMP   L2E218    Abort with CY=1
*
******************************
* DELETE CHARACTER IN BUFFER *
******************************
*
* Deletes character at cursor position and moves
* the text above this position. No action if past
* car.ret, tab or at or beyond end of text.
*
* Exit: ABCDEHL preserved.
*       CY=1: O.K.
*       CY=0: If past car.ret, tab or beyond 0.
*
EDLCH   PUSH  B
        PUSH  PSW
        PUSH  D
        PUSH  H
        CALL  L2E197    Find current pos in buffer
        JNC   L2E219    Quit if past CR, tab
 REM or 0 with CY=0
        MOV   A,M       Get char
        ORA   A
        JZ    L2E219    If at end of text:
 REM quit with CY=0
        NOP 
        NOP 
        NOP 
        PUSH  H
        LHLD  CURSOR    Get cursor pos addr
        XTHL            preserve it on stack
        CALL  CURDEL    Delete cursor
        XCHG            Original HL in DE
        LHLD  EBUFN     Get input pointer
        DCX   H         Decrement it
        SHLD  EBUFN     And store update input pntr
        MOV   B,H       ) Move updated pointer
        MOV   C,L       ) into BC
        DCX   B         Decrement it once again
        XCHG            restore original HL
        CALL  MOVES     Move screen area above
 REM downwards
        CPI   :0D       Deleted car.ret ?
        CNZ   L2E216    If not: reprint text line
        CZ    L2E217    Else: reprint full screen
        JMP   LC232     Put cursor on screen, abort
 REM with CY=1
*
*
*
        UNL
EWINX   EQU   :00A8
SCXBUF  EQU   :00A3
L2E180  EQU   :ECDA
EWRT    EQU   :ECF8
CURSOR  EQU   :0072
CURDEL  EQU   :E36B
L2E189  EQU   :ED6F
EBUFR   EQU   :00A2
CURPT   EQU   :00AE
ECURY   EQU   :00AC
CURLB   EQU   :00B2
EWINY   EQU   :00A9
SUBDE   EQU   :E6F2
HLMUL   EQU   :EB46
CHS     EQU   :008A
CURLS   EQU   :00B0
LD19    EQU   :D1FD
TABTP   EQU   :00B4
TICC8   EQU   :FFF8
LD15    EQU   :D1D1
GAE     EQU   :008C
COMP    EQU   :E6FB
XRET    EQU   :E138
LC216   EQU   :CE85
EBUFS   EQU   :00A6
EBUFN   EQU   :00A4
MOVES   EQU   :E6C2
CURSET  EQU   :E330
ECDN    EQU   :EDAB
LC232   EQU   :CEF2
        LST
        END 
